﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

using NUnit.Framework;

using Refix.Core.Commands;

namespace Refix.Core.Tests.Commands
{
    [TestFixture]
    public class SolutionTest : TestSolutionFolderTest
    {
        public override void SetUp()
        {
            base.SetUp();
            string directoryName = Path.Combine(SolutionRootDirectory.FullName,
                                           "Refix.TestSolution\\Refix");
            if (Directory.Exists(directoryName)) Directory.Delete(directoryName, true);
        }

        [TearDown]
        public void TearDown()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("clean");
            queue.Enqueue("-a");
            queue.Enqueue("Refix.TestSolution.sln");
            Directory.SetCurrentDirectory(Path.Combine(SolutionRootDirectory.FullName, "Refix.TestSolution"));
            Command command = new CommandWithOptions(queue).Command;
            command.Validate();
            command.Execute();
        }

        [Test]
        public void Solution_AddsPreAndPostBuildProjectFiles()
        {
            string preBuildFileName = Path.Combine(SolutionRootDirectory.FullName,
                                           "Refix.TestSolution\\Refix\\Refix.TestSolution.PreBuild.csproj");
            string postBuildFileName = Path.Combine(SolutionRootDirectory.FullName,
                                           "Refix.TestSolution\\Refix\\Refix.TestSolution.PostBuild.csproj");
            Assert.False(File.Exists(preBuildFileName));
            Assert.False(File.Exists(postBuildFileName));
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("sln");
            queue.Enqueue("Refix.TestSolution.sln");
            Directory.SetCurrentDirectory(Path.Combine(SolutionRootDirectory.FullName, "Refix.TestSolution"));
            Command command = new CommandWithOptions(queue).Command;
            command.Validate();
            command.Execute();
            Assert.True(File.Exists(preBuildFileName));
            Assert.True(File.Exists(postBuildFileName));
        }

        [Test]
        public void Solution_ErrorsOnSecondRunWithoutOutput()
        {
            string preBuildFileName = Path.Combine(SolutionRootDirectory.FullName,
                                           "Refix.TestSolution\\Refix\\Refix.TestSolution.PreBuild.csproj");
            string postBuildFileName = Path.Combine(SolutionRootDirectory.FullName,
                                           "Refix.TestSolution\\Refix\\Refix.TestSolution.PostBuild.csproj");
            Assert.False(File.Exists(preBuildFileName));
            Assert.False(File.Exists(postBuildFileName));
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            Directory.SetCurrentDirectory(Path.Combine(SolutionRootDirectory.FullName, "Refix.TestSolution"));
            Command command = new CommandWithOptions(new[] {"sln", "Refix.TestSolution.sln"}).Command;
            command.Validate();
            command.Execute();
            Assert.True(File.Exists(preBuildFileName));
            Assert.True(File.Exists(postBuildFileName));
            Assert.False(command.Execute(), "Soltuion command should have failed second time.");
        }

        [Test]
        public void Solution_RunsASecondTimeWithOutput()
        {
            string preBuildFileName = Path.Combine(SolutionRootDirectory.FullName,
                                           "Refix.TestSolution\\Refix\\Refix.TestSolution.PreBuild.csproj");
            string postBuildFileName = Path.Combine(SolutionRootDirectory.FullName,
                                           "Refix.TestSolution\\Refix\\Refix.TestSolution.PostBuild.csproj");
            Assert.False(File.Exists(preBuildFileName));
            Assert.False(File.Exists(postBuildFileName));
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            Directory.SetCurrentDirectory(Path.Combine(SolutionRootDirectory.FullName, "Refix.TestSolution"));
            Command command = new CommandWithOptions(new[] { "sln", "Refix.TestSolution.sln" }).Command;
            command.Validate();
            command.Execute();
            Assert.True(File.Exists(preBuildFileName));
            Assert.True(File.Exists(postBuildFileName));
            string fixedFile = Path.Combine(SolutionRootDirectory.FullName,
                                            "Refix.TestSolution.Fixed.sln");
            command = new CommandWithOptions(new[] { "sln", "-o", fixedFile, "Refix.TestSolution.sln" }).Command;
            command.Validate();
            Assert.True(command.Execute(), "Solution command should have passed second time.");
            Assert.True(File.Exists(fixedFile));
            File.Delete(fixedFile);
        }
    }
}
